package com.yumingjiang.expand.postprocessor;

import com.alibaba.fastjson2.JSONObject;
import com.yumingjiang.expand.beancreate.B;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @description: 自定义BeanFactoryPostProcessor处理器，Bean属性值
 * @author: John
 * @version: MyBeanFactoryPostProcessor.java, v1.0 2025年01月05日 22:05
 */
@Slf4j
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    //模拟的站位符号
    private static final String FLAG = "|*#*#*|";
    //模拟修改后的值
    private static final String MODIFY = "modifyValue";

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Map<String, B> beansOfType = beanFactory.getBeansOfType(B.class);
        beansOfType.entrySet().stream().iterator().forEachRemaining(beanInfo ->{
            B b = beanInfo.getValue();
            log.info("[PRE] "+beanInfo.getKey()+"->"+ JSONObject.toJSONString(b));
            log.info("[PRE] value:{}",b.getName());
            b.setName(b.getName().replace(FLAG,MODIFY));
            log.info("[AFTER] bean(b) info:{}",JSONObject.toJSONString(b));
        });
    }
}

